from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer

def datasets_demo():
    """
    sklearn数据集使用
    """

    # 获取数据集
    iris = load_iris()
    print("鸢尾花数据集：\n", iris)
    print("鸢尾花数据集：\n", iris["DESCR"])

    print(type(iris))
    # 数据集划分

    x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=22)
    print("训练集的特征值：\n",x_train,x_train.shape)
    print("训练集的测试值：\n",x_test,x_test.shape)
    return None

def dict_demo():
    """
    字典的特征提取
    """
    data = [{'city': '北京', 'temperature':100},{'city': '上海', 'temperature':60},{'city': '广州', 'temperature':30}]
    # 1、实例化一个转换器类 默认 sparse 是为稀疏矩阵（非0值按位置表示）  否为数组
    """
         Coords	Values
          (0, 1)	1.0
          (0, 3)	100.0
          (1, 0)	1.0
          (1, 3)	60.0
          (2, 2)	1.0
          (2, 3)	30.0
    
         [[  0.   1.   0. 100.]
         [  1.   0.   0.  60.]
         [  0.   0.   1.  30.]]
    """
    transfer = DictVectorizer(sparse=False)
    # 2、调用fit_transform()
    data_new = transfer.fit_transform(data)
    print("data_new:\n",data_new)
    print("特征名字：\n",transfer.get_feature_names_out())
    return None

if __name__ == "__main__":
    # 代码1： sklearn数据集使用
    # datasets_demo()
    
    # 代码2：字典特征抽取
    dict_demo()